package com.lanou.dllo.mynovel.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.mynovel.R;
import com.lanou.dllo.mynovel.bean.StoreUpdate;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class StoreUpdateAdapter extends
        RecyclerView.Adapter<StoreUpdateAdapter.StoreUpdateHolder> {

    private List<StoreUpdate.DataEntity.ReturnDataEntity> returnData; // 存放数据的集合
    private List<String> tags; // 分类类型
    private RequestQueue requestQueue; // 定义一个volley请求队列
    private int type = 0;

    public StoreUpdateAdapter(Context context) {
        // 初始化请求队列
        requestQueue = Volley.newRequestQueue(context);
    }

    public void addData(List<StoreUpdate.DataEntity.ReturnDataEntity> datas) {
        returnData = datas;
        notifyDataSetChanged(); // 改变数据 通知刷新
    }

    public void addData(List<StoreUpdate.DataEntity.ReturnDataEntity> datas, int type) {
        returnData = datas;
        this.type = type;
        notifyDataSetChanged(); // 改变数据 通知刷新
    }


    class StoreUpdateHolder extends RecyclerView.ViewHolder {

        private TextView name; // 漫画名
        private ImageView cover; // 漫画封面图片
        private TextView nickname; // 作者名
        private TextView classifyName; // 漫画类型
        private TextView click_total; // 点击量
        private TextView last_update_time; // 更新时间
        private TextView last_update_chapter_name; // 更新最新章节名字


        public StoreUpdateHolder(View view) {
            super(view);

            cover = (ImageView) view.findViewById(R.id.cover);
            name = (TextView) view.findViewById(R.id.name);
            nickname = (TextView) view.findViewById(R.id.nickname);
            classifyName = (TextView) view.findViewById(R.id.classifyName);
            click_total = (TextView) view.findViewById(R.id.click_total);
            last_update_time = (TextView) view.findViewById(R.id.last_update_time);
            last_update_chapter_name = (TextView) view.findViewById(R.id.last_update_chapter_name);

        }
    }

    @Override
    public StoreUpdateHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 获得布局加载器
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        // 加载行布局文件
        View view = inflater.inflate(R.layout.fragment_storeupdate_content, null);
        // 构建组件缓存对象并返回
        return new StoreUpdateHolder(view);
    }

    /**
     * 通过volley请求图片
     **/
    public void getBitmap(String url, final ImageView imageView, int w, int h) {

        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        // 将返回的bitmap图片对象设置到图片组件中
                        imageView.setImageBitmap(response);
                    }
                },
                w, h, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // 打印错误信息
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        requestQueue.add(request); // 将本次图片请求加入到请求列队中

    }


    @Override
    public void onBindViewHolder(StoreUpdateHolder holder, int position) {
        if (returnData != null && returnData.size() > 0) {
            StoreUpdate.DataEntity.ReturnDataEntity entity = returnData.get(position);

            // 设置数据
            holder.name.setText(entity.getName());
            holder.nickname.setText(entity.getNickname());
            holder.click_total.setText(entity.getClick_total());
            holder.last_update_time.setText(updateTime(entity.getLast_update_time()));
            holder.last_update_chapter_name.setText(entity.getLast_update_chapter_name());
            if (type == 0) {
                List<String> tags = entity.getTags();
                switch (tags.size()) {
                    case 1:
                        holder.classifyName.setText(entity.getTags().get((0)));
                        break;
                    case 2:
                        holder.classifyName.setText(entity.getTags().get((0)) + " " + entity.getTags().get((1)));
                        break;
                    case 3:
                        holder.classifyName.setText(entity.getTags().get((0)) + " " + entity.getTags().get((1)) + " " + entity.getTags().get((2)));
                        break;
                }
            } else {
                holder.classifyName.setText(entity.getTags().get((0)));
            }
            // 进行图片请求前先判断返回的集合中是否由图片
            if (entity.getCover() != null) {
                String imgUrl = entity.getCover();
                getBitmap(imgUrl, holder.cover, 1000, 500);
            }
        }
    }

    // 漫画更新时间
    public String updateTime(int t) {

        // 时间格式化
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 HH时mm分");
        Long time = System.currentTimeMillis();
        Date curDate = new Date(Long.parseLong(t + "000"));//获取当前时间
        String str = formatter.format(curDate);
        return str;
        // 时间格式化
//        SimpleDateFormat formatter = new SimpleDateFormat("HH小时mm");
//        Log.i("time---", formatter.format(new Date(t)));
//        Date upTime = new Date(Long.parseLong(t + "000"));
//        String nowstime = formatter.format(new Date(t)) + "";
//        return nowstime;

//        SimpleDateFormat formatter = new SimpleDateFormat("HH小时mm");
//        Long time = System.currentTimeMillis();
//        String uptime = t + "000";
//        Date upTime = new Date(Long.parseLong(uptime));
//        Date nowsTime = new Date(time);
//        uptime = formatter.format(upTime) + "";
//        String nowstime = formatter.format(nowsTime) + "";
//        SimpleDateFormat formatter1 = new SimpleDateFormat("mm分钟");
//        if (uptime.substring(0, 2).equals(nowstime.substring(0, 2))) {
//            upTime.setTime(nowsTime.getTime() - upTime.getTime());
//            String str = formatter1.format(upTime);
//            if (Integer.parseInt(str) < 0) {
//                str = 0 - Integer.parseInt(str) + "";
//            }
//            return str;
//        } else {
//            int upDataTime = Integer.parseInt(nowstime.substring(0, 2)) - Integer.parseInt(uptime.substring(0, 2));
//            if (upDataTime < 0) {
//                upDataTime = 0 - upDataTime;
//            }
//            return upDataTime + "小时";
//        }
    }

    @Override
    public int getItemCount() {
        return returnData != null && returnData.size() > 0 ? returnData.size() : 0;
    }
}
